package database;

import java.sql.*;

public class DBConnector {
	public boolean checkLogin(String username, String password)
	{
		Connection c = null;
	    Statement stmt = null;
	    String name = null;
	    String pass = null;
	    if (username == null || password == null) return false;
	    try {
	      Class.forName("org.sqlite.JDBC");
	      c = DriverManager.getConnection("jdbc:sqlite:database.db");
	      c.setAutoCommit(false);
	      //debug System.out.println("Opened database successfully");
	      
	      stmt = c.createStatement();
	      //System.out.println("SELECT * FROM users WHERE username = \'" + username + "\';");
	      ResultSet rs = stmt.executeQuery( "SELECT * FROM users WHERE username = \'" + username + "\';" );
	      
	      while ( rs.next() ) {
	         int id = rs.getInt("id");
	         name = rs.getString("username");
	         pass = rs.getString("password");
	      }
	      rs.close();
	      stmt.close();
	      c.close();
	    } catch ( Exception e ) {
	      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
	      return false;
	    }
	    //DEBUG
	   // System.out.println(name + " " + pass);
	   // System.out.println(username + " " + password);
	    if (name == null || pass == null) return false;
	    if (username.contentEquals(name) && password.contentEquals(pass)) {
	    	//System.out.println("We got a match!");
	    	return true;
	    }
		return false;
	}

}
